<?
class iMVC_DB_Mysql_Writer extends iMVC_DB_Writer_Abstract{

    private $_mysqli;

    function __construct($mysqli){
        $this->_mysqli=$mysqli;
    }
    public function insert($table,$properties){

        $keys=array();
        $values=array();
        foreach($properties as $key=>$value){
        
            $keys[]=$key; 
            if(is_string($value)){
                $values[]="'".$value."'";
            }
            else{
                $values[]=$value;
            }
        }
        $keyStr=implode(',',$keys);
        $valStr=implode(',',$values);
        $sql=sprintf('INSERT INTO %s (%s)VALUES(%s)',$table,$keyStr,$valStr);
        $rs=$this->_mysqli->query($sql);
        return $rs ;

    }
    public function deleteByPk($table,$pk,$pk_value){

        
        $sql=sprintf('delete from %s where %s=%s',$table,$pk,$pk_value);
        $rs=$this->_mysqli->query($sql);
        return $rs ;
    
    }

    public function updateByPk($table,$pk,$properties){

        $str='';
        $isFirst=true;

        foreach($properties as $key=>$value){

            if($key!=$pk){
                $str.=$isFirst?' ':',';
                $str.="".$key."=";
                if(is_string($value)){
                    $str.="'".$value."'";
                }
                else{
                    $str.=$value;
                }
                $isFirst=false;
            }
        }

        $sql=sprintf('UPDATE %s SET %s where %s=%s',$table,$str,$pk,$properties[$pk]);
        $rs=$this->_mysqli->query($sql);
        return $rs ;

    
    
    }

}


// vim600:ts=4 st=4 foldmethod=marker foldmarker=<<<,>>>
// vim600:syn=php commentstring=//%s
